<?php
declare(strict_types=1);

namespace Modules\Notice\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Notice\Models\Notice;
use Illuminate\Http\Request;


class NoticeController extends Controller
{
    public function __construct(
        protected readonly Notice $model
    ){}

    public function index(Request $request): mixed
    {
        $params = $request->all();
        $notice = Notice::query()->select()->paginate($params['limit'] ?? 10 ,['*'] ,'' ,$params['page'])->toArray();

        foreach ($notice['data'] as &$item)
        {
            $item['created_at'] = date('Y-m-d H:i:s', strtotime($item['created_at']));
        }

        return $notice;
    }

    public function add(Request $request): mixed
    {
        $data = $request->all();
        return $this->model->createBy($data);
    }

    public function update(Request $request): mixed
    {
        $data = $request->all();
        return $this->model->updateBy($data['id'], $data);
    }

    public function delete(Request $request): mixed
    {
        $data = $request->all();
        return $this->model->deleteBy($data['id']);
    }
}
